home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / delphi.swg / 0153_Re: Popup Menus at runtime.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-08-30  |  2.0 KB  |  71 lines

  1.  
  2. {
  3. > Can you add menu items to the standard popup menu of a form?
  4. > I want to do the same thing that Borland did for their PageControl
  5. > component.  If you right-click on the Page Control, you get the same std
  6. > menu but with a couple of new items inserted at the top (e.g. New Page).
  7. > I remember reading a post asking about how to create menus at runtime.
  8. > I don't think it was answered through the newsgroup, so...
  9. > How do you create menus at runtime?
  10.  
  11. Do you mean add things to the right click menu when designing? Or create a popup 
  12. when the program is running? 
  13.  
  14. When running simply create a TPopupMenu and assign it to the PopupMenu property.
  15. This can either be done at design-time or run-time.  It won't show at design time 
  16. however, since that menu is the form designers.
  17.  
  18. To add items to the context menu that is shown at design-time, you need to create 
  19. a new component editor for your component.
  20.  
  21. You'll need to override GetVerbCount to return the number of items you want to
  22. add to the menu, and GetVerb to actually fill the list of items.  You'll also
  23. need to override ExecuteVerb to actually do the actions.
  24.  
  25. For example:
  26. }
  27.  
  28. TMyEditor = class(TComponentEditor)
  29. public
  30.   function GetVerbCount : Integer; override;
  31.   function GetVerb(Index : Integer); override;
  32.   procedure ExecuteVerb(Index : Integer); override;\
  33. end;
  34.  
  35. function TMyEditor.GetVerbCount : Integer;
  36. begin
  37.      Result := 2;
  38. end;
  39.  
  40. function TMyEditor.GetVerb(Index : Integer);
  41. begin
  42.   case Index of
  43.         0 : Result := 'Say Hi';
  44.          1 : Result := 'Say Bye';
  45.   end;
  46. end;
  47.  
  48. procedure TMyEditor.ExecuteVerb(Index : Integer);
  49. var
  50.    MyMsg : String;
  51. begin
  52.      case Index of
  53.         0 : MyMsg := 'Hi';
  54.         1 : MyMsg := 'Bye;
  55.      end;
  56.      MessageDlg(MyMsg,mtInformation,[mbOK],0);
  57. end;
  58.  
  59. You'll also need to register the component editor in the Register proc for the 
  60. component.
  61.  
  62. procedure Register;
  63. begin
  64.      RegisterComponents('My Component Page',[TMyComponent]);
  65.      RegisterComponentEditor(TMyComponent,TMyEditor);
  66. end;
  67.  
  68.